Skip to content

fix(grok): stop the Windows status hook from spawning two interpreters per event - #15595

Merged
nwparker merged 1 commit into
mainfrom
fix/grok-windows-hook-interpreter-chain
Aug 21, 2026
Merged

nwparker merged 1 commit into
mainfrom
fix/grok-windows-hook-interpreter-chain

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​199 $\color{#cf222e}{\Huge{\mathbf{−}}}$​10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​189
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​16 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​14

ELI5

On Windows, Grok runs any hook command that contains a space by handing it to PowerShell 7
(pwsh -Command …). Orca registered its Grok status hook as an encoded powershell.exe …
command line, which is full of spaces — so every status event started two interpreters
before it reached a script whose whole job is one loopback POST:

before   grok.exe -> pwsh.exe -> powershell.exe -> cmd.exe -> curl.exe     518-719 ms
after    grok.exe -> cmd.exe -> curl.exe                                    43-213 ms

Orca now registers the script's own path, the same way Codex, Devin and Antigravity hooks are
already registered. Grok spawns it directly, the two interpreters disappear, and the status
posts are byte-for-byte the same.

What Changed

  • src/main/grok/hook-service.ts — the Windows managed command comes from the shared
    wrapWindowsCmdHookCommand(scriptPath) instead of wrapWindowsHookCommand(scriptPath). That
    helper returns the bare path when it is cmd-safe and otherwise falls back to the same encoded
    PowerShell launcher as before, so [Bug]: Codex hooks fail on Windows when user path contains spaces #6078 (space in the profile path) stays covered.
  • src/main/grok/windows-hook-launcher-chain.test.ts — new. Pins the launch shape: the
    registered command is the script path itself, names a real file, contains no whitespace (the
    sole trigger for Grok's pwsh wrapper), and carries no interpreter. Also covers the upgrade
    sweep and the non-cmd-safe fallback. Split into synthetic-path cases that run on every CI
    leg and install-based cases gated to win32 — only process.platform can be mocked, not
    path.join, so a real install on a POSIX runner yields a /-separated path that routes to
    the [Bug]: Codex hooks fail on Windows when user path contains spaces #6078 fallback by design (same gating and reason as installer-utils.test.ts:692).
  • src/main/grok/hook-service.ts — small getManagedCommandForTests seam, so the
    platform-independent cases assert the decision this PR makes rather than the shared helper.
  • src/main/grok/hook-service.test.ts — the two assertions that hard-coded the PowerShell
    launcher now accept either registration; which branch applies is pinned by the new file.

No change to the script body, the POSIX script, the remote/SSH installer, the WSL relay path,
or any other agent.

Why

#14828 reports a console flash and focus steal on every Grok tool call. Three reporters
posted the same process tree — grok.exe -> pwsh.exe/powershell.exe -> cmd.exe. I reproduced
that tree exactly by instrumenting a managed hook to log its own ancestor chain while the real
Grok CLI ran in a real ConPTY:

current shape   cmd.exe(28880)  <- powershell.exe(114684) <- pwsh.exe(100120) <- grok.exe
bare .cmd       cmd.exe(123544) <- grok.exe

Grok's own debug log names the mechanism: xai_grok_config::shell: Windows shell: pwsh.

Measured with Grok 1.0.5's own dispatcher timing, both variants registered in the same session,
running a byte-identical copy of the real grok-hook.cmd against a live listener:

per hook event
current (encoded PowerShell launcher) 518, 565, 625, 719 ms — mean 607 ms
bare .cmd path 43, 53, 55, 133, 213 ms — mean 114 ms

A controlled A/B outside Grok (12 spawns each, both delivering 12/12 posts) reproduces the
ratio: 215 ms vs 49 ms.

Orca installs nine Grok events, and one single-tool-call prompt dispatched ten of them. This is
the mechanism the merged Antigravity fix (#15520) already established for this class of bug —
Orca cannot suppress the console the agent allocates for a hook, but it controls how long that
console lives. Grok was the worse case, paying two interpreters rather than one.

What I could not reproduce

I could not make a visible console window appear from the Grok hook path on this hardware
(Windows 11 26200, Orca main, Grok 1.0.5), in either shape, across ~30 real hook events with a
5 ms window sampler running. The reason is measurable: Grok creates hook processes with
CREATE_NO_WINDOW. Verified by discriminating the flags from inside a hook —

creation flag what the hook process sees
CREATE_NO_WINDOW console=NONE
DETACHED_PROCESS console=2361644 visible=True class=PseudoConsoleWindow

Grok's hooks report console=NONE at every level of the chain, so they have a console with no
window and every child inherits it. This is also the check that says the change is safe:
DETACHED_PROCESS is the one mode where a bare .cmd would be worse than the PowerShell
launcher (cmd.exe must allocate a console; PowerShell does not), and Grok demonstrably does not
use it.

So I am not claiming this makes a window I observed go away. I am claiming it removes the
interpreter chain that determines how long such a window can exist, on the same reasoning and
in the same shape as the fix already merged for Antigravity.

Community PRs

Two open PRs target this issue. I took neither, but both contributed to the diagnosis:

  • fix(windows): spawn Grok status hooks via a GUI launcher #15530 (@sanshengai) — correctly identified that "Grok treats any hook command that
    contains a space as pwsh -Command …"
    . That is the load-bearing insight here and my
    instrumentation confirms it. Their fix ships a new GUI-subsystem orca-grok-hook.exe plus a
    native build step and packaging changes; registering the existing .cmd removes the same
    pwsh wrapper with no new binary in the shipped build.
  • fix(agent-hooks): hide Windows .cmd hook consoles without dropping stdout #15506 (@ynjmxn) — supplied the accurate process tree that this PR reproduces. Their fix
    keeps the encoded PowerShell wrapper and replaces & $scriptPath with ProcessStartInfo +
    CreateNoWindow, which leaves both interpreters and the ~607 ms in place. Their stated
    premise — that the .cmd has no console to inherit so Windows creates one — is the
    DETACHED_PROCESS case above, which I could not reproduce under Grok's actual flags.

Not this issue, but found while looking

node-pty's ConPTY teardown forks conpty_console_list_agent with no windowsHide
(windowsPtyAgent.js:184). From a host with no console that pops a focus-stealing Windows
Terminal window on every kill() — reproduced 3/3, with 0/2 on natural exit. Orca is not
affected because it pins useConptyDll: true, which skips that branch entirely (verified 0/3).
Recording it on the issue rather than changing anything here.

Linked Issue

Fixes #14828

Visual Proof

N/A for rendered Orca UI — no Orca view, layout, or in-app control changes. The subject is an
OS console belonging to the agent's own process tree, so the evidence is process-level and
lives in Why: measured ancestor chains, Grok's own dispatcher timings, and the creation-flag
discriminator.

Testing

  • src/main/grok/ — 29 passed. Verified as a real guard, not a tautology: against the
    pre-fix code the new suite fails and names the defect (expected 'C:/WINDOWS/…powershell.exe -NoProfile…' to be 'C:\…\grok-hook.cmd'), including the
    synthetic-path case that runs on the POSIX legs.

  • src/main/agent-hooks/ + src/main/grok/ — 649 passed, 6 failures that are pre-existing on
    this box
    : 3 × installer-utils symlink, 2 × Claude managed-hook-stdin-lifecycle, 1 ×
    Claude windows-hook-payload-delivery. Confirmed identical on a clean HEAD worktree — same
    6, none in src/main/grok/, none touching this change. (Same set fix(agent-hooks): stop Antigravity's Windows hook from spawning PowerShell on every event #15520 recorded.)

  • pnpm typecheck clean. oxlint --deny-warnings clean. oxfmt --check clean on the content
    Git stores (the local checkout is CRLF, which flags files this PR does not touch).

  • Live Windows, real Grok CLI: installed the fixed shape and ran a two-tool-call prompt —
    9/9 status posts delivered with full payloads (607–1538 bytes), hook events completing in
    43–121 ms.

  • Missing-script behaviour (the one contract the encoded launcher provided and a bare path
    cannot): registered a nonexistent script and ran a tool call. Grok fails open — logs
    hook failed … hook_failure=command not found: … at elapsed_ms=0 and the tool still
    runs
    , including for pre_tool_use, which Grok declares a blocking event. Same posture
    Codex/Devin/Antigravity already have, and install() writes the script and the config
    together.

  • I manually tested these changes locally

  • Automated tests added/updated

Review

Ran /code-review high over the branch. It raised three findings; all are addressed:

  • HIGH — the cmd-safe cases would fail on the Linux CI shard. Correct, and I had reached
    the same conclusion independently: only process.platform is mockable, so path.join still
    produces /tmp/…, which is not cmd-safe and takes the fallback. Restructured as described in
    What Changed.

  • MEDIUM — no cross-platform pin on the launch shape. Fixed by the getManagedCommandForTests
    seam; the pin now runs on every leg and is verified to fail against the pre-fix code.

  • LOW — the bare path drops the launcher's missing-script guard. Real, and now stated in the
    code comment, the commit message, and Testing above, with the fail-open behaviour measured
    rather than assumed.

  • Upgrade path: installManagedAgentHooks calls install() unconditionally behind the CLI
    presence gate, and createManagedCommandMatcher decodes encoded commands before matching, so
    an existing -EncodedCommand entry is swept and replaced — including in retired events.
    Covered by a test.

  • Security: the registered value is an absolute path under the user's own profile,
    constrained by WINDOWS_CMD_SAFE_PATH and spawned as argv[0], not through a shell, so there is
    no command line to quote or inject into. No secret moves onto a command line. No new binary,
    dependency, install hook, or network sink.

  • Cross-platform: POSIX branch untouched. installRemote still writes the POSIX .sh over
    SFTP; WSL uses the POSIX script via the relay. Windows validated on hardware.

  • Known gap (unchanged by this PR, worth stating): a profile path that is not cmd-safe —
    spaces, or non-ASCII characters — still takes the encoded-launcher fallback and keeps the old
    cost. That is the shared helper's existing behaviour for Codex/Devin/Antigravity too;
    widening the character class would change those three agents and is deliberately out of scope.

  • Performance: strictly better — two fewer processes and ~490 ms less per hook event, ~10
    events per tool-calling turn.

  • Provider neutrality / UI / mobile / persisted state / wire format: untouched.

Agent skill upstream boundary

  • Not applicable.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…s per event (#14828)

Grok runs a hook `command` containing a space as `pwsh -Command <cmd>`. Orca
registered the encoded PowerShell launcher, whose command line is full of
spaces, so every status event walked

  grok.exe -> pwsh.exe -> powershell.exe -> cmd.exe -> curl.exe

before reaching a script whose entire job is one loopback POST. Measured
against Grok 1.0.5's own dispatcher on Windows 11, that is 518-719ms per event
(mean 607ms). The same script registered as its bare path is spawned directly
as `grok.exe -> cmd.exe` and costs 43-213ms (mean 114ms).

Orca installs nine Grok events, and a single one-tool-call prompt dispatches
roughly ten of them, so this is the difference between a hook chain that is
continuously resident during a turn and one that is not. It is also how long
the console the agent allocates for a hook can stay on screen, which is the
mechanism #15520 fixed for Antigravity -- Grok was worse, paying two
interpreters rather than one.

Use the shared `wrapWindowsCmdHookCommand`, the same registration Codex, Devin
and Antigravity already use (#8430). A profile path that is not cmd-safe still
falls back to the encoded launcher, so #6078 stays covered.

Tradeoff carried over from those agents: a bare path cannot hold the launcher's
missing-script guard, so a deleted script surfaces as a per-event
`command not found` in Grok's log rather than a silent stdin drain. Verified
that Grok fails open there -- the tool still runs, including on PreToolUse,
which Grok declares a blocking event.

Verified end to end against the real Grok CLI: 9/9 status posts delivered with
full payloads through the bare path, and 12/12 in a controlled A/B spawning
both shapes the way Grok does. Also confirmed Grok creates hook processes with
CREATE_NO_WINDOW rather than DETACHED_PROCESS, which is the mode where a bare
.cmd would have been worse than the launcher.
@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Windows Grok hooks now use a cmd-safe command wrapper, with encoded PowerShell fallback for paths that contain spaces. POSIX command handling remains unchanged. A test helper exposes generated managed commands. Tests now cover launcher generation, managed event installation, stale launcher replacement, retired event cleanup, user hook preservation, and platform-independent command matching.

Merge Risk: 🔵 Low · up to 0d2a0

The Windows hook change is narrowly scoped and improves launch performance, but the upgrade-path tests can accept unrelated commands because they match by substring rather than exact command identity. This is a bounded test-correctness risk that should receive owner follow-up.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the primary fix: preventing duplicate interpreter launches for the Windows Grok status hook.
Description check ✅ Passed The description follows the template and documents the change, rationale, issue, testing, platform impact, and checklist status.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/grok/hook-service.ts (1)

105-113: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Shorten the explanatory comments.

Keep each comment to the non-obvious rationale only.

  • src/main/grok/hook-service.ts#L105-L113: reduce the launcher-chain and benchmark explanation to one concise reason.
  • src/main/grok/windows-hook-launcher-chain.test.ts#L1-L6: remove benchmark and dispatch-detail commentary that the test name does not need.
  • src/main/grok/hook-service.test.ts#L28-L30: reduce the fallback description to one line.

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3dfa8ba2-6df9-4811-9040-efb5d562c1c1

📥 Commits

Reviewing files that changed from the base of the PR and between feaebab and 0d2a0e0.

📒 Files selected for processing (3)
  • src/main/grok/hook-service.test.ts
  • src/main/grok/hook-service.ts
  • src/main/grok/windows-hook-launcher-chain.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment on lines +31 to +33
function registersManagedGrokScript(command: string): boolean {
return command.includes(GROK_SCRIPT_FILE_NAME) || WINDOWS_POWERSHELL_LAUNCHER.test(command)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Match the expected managed command exactly.

command.includes(GROK_SCRIPT_FILE_NAME) accepts user commands such as echo grok-hook.sh. WINDOWS_POWERSHELL_LAUNCHER also accepts unrelated encoded PowerShell hooks. Lines 263 and 372 can pass when no managed hook is installed. Derive the expected command from the known script path, then compare commands for equality.

@ynjmxn

ynjmxn commented Aug 20, 2026

Copy link
Copy Markdown

Thanks for taking this on. Closing #15506 in favor of this PR.

@nwparker
nwparker merged commit 8b60a1e into main Aug 21, 2026
48 checks passed
@nwparker
nwparker deleted the fix/grok-windows-hook-interpreter-chain branch August 21, 2026 02:11
mcxinyu pushed a commit to mcxinyu/orca that referenced this pull request Aug 22, 2026
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 23, 2026
dallascrilley pushed a commit to dallascrilley/orca that referenced this pull request Aug 27, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Windows: Grok agent hooks flash a visible console and steal keyboard focus on every tool call

3 participants